package com.hdsx.spring.tomcat.response;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

/**
 * @author 占贤银
 * time 2024/8/16 15:10
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
public class HttpServletResponse {

    private ChannelHandlerContext ctx;

    public HttpServletResponse(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    public void write(String content) {
        if (content == null || content.length() == 0) {
            return ;
        }
        FullHttpResponse response =
                new DefaultFullHttpResponse
                        (HttpVersion.HTTP_1_1,
                                HttpResponseStatus.OK,
                                Unpooled.wrappedBuffer(content.getBytes(CharsetUtil.UTF_8)));
        response.headers().set("Content-Type", "text/html;charset=utf-8");
        response.headers().set("Content-Length", content.getBytes(CharsetUtil.UTF_8).length);
        response.headers().set("Content-Language", "zh-CN");
        response.headers().set("Connection", "keep-alive");
        ctx.write(response);
        ctx.flush();
        ctx.close();
    }
}